package thread;

//setDaemon()

public class Demo7 {
    public static void main(String[] args) throws InterruptedException {
        Thread t =new Thread(() ->{
            while(true){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        //将t线程设置成后台线程，在主线程结束后，该线程随之结束
        //这个设置要在（start）开始线程之前
        t.setDaemon(true);
        t.start();

        for (int i = 0; i < 3; i++) {
            System.out.println("main thread");
            Thread.sleep(1000);
        }
        System.out.println("main thread end");
    }
}
